﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample1213
{
    public class LandData
    {
        /// <summary>
        /// 顶点数
        /// </summary>
        public uint VCount { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public float[] GenData(int width, int height)
        {
            int landSpan = 30;
            int rows = height - 1;
            int cols = width - 1;
            int gzCount = rows * cols;
            VCount = (uint)gzCount * 2 * 3;
            float[] vData = new float[VCount * 7];
            float sSpan = 8.0f / cols;
            float tSpan = 8.0f / rows;
            float xStart = -landSpan * cols / 2.0f;
            float zStart = -landSpan * rows / 2.0f;
            float ssSpan = 1.0f / cols; //计算山地高度的纹理S 坐标跨度
            float ttSpan = 1.0f / cols; //计算山地高度的纹理T 坐标跨度
            int indexTemp = 0; //辅助索引
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    float x0 = xStart + landSpan * i; //当前网格0 号顶点X 坐标
                    float y0 = 0; //当前网格0 号顶点Y 坐标
                    float z0 = zStart + landSpan * j; //当前网格0 号顶点Z 坐标
                    float s0 = sSpan * i; //当前网格0 号顶点纹理S 坐标(外观)
                    float t0 = tSpan * j; //当前网格0 号顶点纹理T 坐标(外观)
                    float ss0 = ssSpan * i; //当前网格0 号顶点纹理S 坐标(高度)
                    float tt0 = ttSpan * j; //当前网格0 号顶点纹理T 坐标(高度)

                    float x1 = xStart + landSpan * (i + 1);
                    float y1 = 0;
                    float z1 = zStart + landSpan * j;
                    float s1 = sSpan * (i + 1);
                    float t1 = tSpan * j;
                    float ss1 = ssSpan * (i + 1);
                    float tt1 = ttSpan * j;

                    float x2 = xStart + landSpan * i;
                    float y2 = 0;
                    float z2 = zStart + landSpan * (j + 1);
                    float s2 = sSpan * i;
                    float t2 = tSpan * (j + 1);
                    float ss2 = ssSpan * i;
                    float tt2 = ttSpan * (j + 1);

                    float x3 = xStart + landSpan * (i + 1);
                    float y3 = 0;
                    float z3 = zStart + landSpan * (j + 1);
                    float s3 = sSpan * (i + 1);
                    float t3 = tSpan * (j + 1);
                    float ss3 = ssSpan * (i + 1);
                    float tt3 = ttSpan * (j + 1);

                    vData[indexTemp++] = x0; //将0 号顶点X 坐标存入结果数组
                    vData[indexTemp++] = y0; //将0 号顶点Y 坐标存入结果数组
                    vData[indexTemp++] = z0; //将0 号顶点Z 坐标存入结果数组
                    vData[indexTemp++] = s0; //将0 号顶点S 纹理坐标存入结果数组(外观)
                    vData[indexTemp++] = t0; //将0 号顶点T 纹理坐标存入结果数组(外观)
                    vData[indexTemp++] = ss0; //将0 号顶点S 纹理坐标存入结果数组(高度)
                    vData[indexTemp++] = tt0; //将0 号顶点T 纹理坐标存入结果数组(高度)
                    vData[indexTemp++] = x3;
                    vData[indexTemp++] = y3;
                    vData[indexTemp++] = z3;
                    vData[indexTemp++] = s3;
                    vData[indexTemp++] = t3;
                    vData[indexTemp++] = ss3;
                    vData[indexTemp++] = tt3;
                    vData[indexTemp++] = x1;
                    vData[indexTemp++] = y1;
                    vData[indexTemp++] = z1;
                    vData[indexTemp++] = s1;
                    vData[indexTemp++] = t1;
                    vData[indexTemp++] = ss1;
                    vData[indexTemp++] = tt1;
                    vData[indexTemp++] = x0;
                    vData[indexTemp++] = y0;
                    vData[indexTemp++] = z0;
                    vData[indexTemp++] = s0;
                    vData[indexTemp++] = t0;
                    vData[indexTemp++] = ss0;
                    vData[indexTemp++] = tt0;
                    vData[indexTemp++] = x2;
                    vData[indexTemp++] = y2;
                    vData[indexTemp++] = z2;
                    vData[indexTemp++] = s2;
                    vData[indexTemp++] = t2;
                    vData[indexTemp++] = ss2;
                    vData[indexTemp++] = tt2;
                    vData[indexTemp++] = x3;
                    vData[indexTemp++] = y3;
                    vData[indexTemp++] = z3;
                    vData[indexTemp++] = s3;
                    vData[indexTemp++] = t3;
                    vData[indexTemp++] = ss3;
                    vData[indexTemp++] = tt3;
                }
            }
            return vData;
        }
    }
}
